import {Component, EventEmitter, Input, OnDestroy, OnInit, Output} from "@angular/core";
import {CommService} from "../comm-service";
import {SharedService} from "../shared-service";
import {LocalStorageService} from "../local-storage-service";
import {MyhttpService} from "../myhttp.service";

@Component({
  template: `
    <div class="bmssjxx comp" [style.width]="compw" [style.height]="comph" [style.top]="topv"
         [style.left]="leftv">
      <div class="content">
        <div class="title">
          <div class="title_text">试卷情况{{comptitle}}</div>
        </div>
        <div class="body">
          <div class="kdsbjRoom iecrollkf chromescrollkf" *ngIf="kdsjbList.length">
            <div class="mytr" *ngFor="let row of kdsjbList;">
              <div class="fl mycell" style="background-color: #CDE7FE">
                <div class="text1">{{row.kdmc}}</div>
                <div class="text2">
                  <span>总数：</span>
                  <span>{{row.zs}}</span>
                </div>
              </div>
              <div class="fl mycell">
                <div class="text1">
                  <span>应在保密室：</span>
                  <span class="yzcolor">{{row.yz_bms}}</span>
                </div>
                <div class="text2">
                  <span>应在考点：</span>
                  <span class="yzcolor">{{row.yz_kd}}</span>
                </div>
              </div>
              <div class="fl mycell">
                <div class="text1">
                  <span>实在保密室：</span>
                  <span class="szcolor">{{row.yz_bms}}</span>
                </div>
                <div class="text2">
                  <span>实在考点：</span>
                  <span class="szcolor">{{row.yz_kd}}</span>
                </div>
              </div>
              <div class="fl mycell">
                <div class="text1">
                  <span>缺失：</span>
                  <span class="qscolor">{{row.qs_bms}}</span>
                </div>
                <div class="text2">
                  <span>缺失：</span>
                  <span class="qscolor">{{row.qs_kd}}</span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  `,
  styles: [
    ".bmssjxx{position:absolute;background-color:#fff;border:5px solid #ccc;}",
    ".content{width: calc(100%);height: calc(100%);}",
    ".content>.title{position: relative;width: 100%;height: auto;padding: 10px; border-bottom: 1px solid #ccc;color: rgb(138 143 151);}",
    ".title>.title_text{height: 15px;line-height: 15px;font-size: 15px;  width: auto;border-left: 5px solid #1477F3;padding-left: 10px}",
    ".content>.body{position: relative;width: 100%;height: calc(100% - 24px);padding: 10px 10px}",
    ".kdsbjRoom{width: calc(100% - 17px);height: 283px;overflow-y: auto}",
    ".mytr{margin-bottom: 10px;height: 131px;background-color: red}",
    ".mycell{width: 25%;height: 100%;background-color: #F5FAFF;padding: 20px 0px;}",
    ".text1{width: 100%;height: 50px;line-height:50px;text-align: center}",
    ".text2{width: 100%;height: 50%;text-align: center}",
    ".yzcolor{color: #2691f1}",
    ".szcolor{color: #4caf50}",
    ".qscolor{color: #fe5722}"
  ]
})
export class BmskdBmssjxxComponent implements OnInit, OnDestroy {
  compw: string = "0px";
  comph: string = "0px";
  topv: string = "0px";
  leftv: string = "0px";
  comptitle: string = "";
  frompage: string;
  s_ksjhbh: string = "";
  s_kdbhlist: any[] = [];
  kdsjbList: any[] = [];

  lockid: any;
  intervalnum: number = 30000;


  getdata(): void {
    if (!this.s_kdbhlist.length) {
      return
    }
    const url = "sjgz/zsymbmsjkd/kdsjbqktj";
    this.itservice.kwPtPost(url, {
      ksjhbh: this.s_ksjhbh,
      kdbhList: this.s_kdbhlist
    }).subscribe(resp => {
      this.commservice.handResponse("get", url, resp, (arg1, resp, arg2) => {
        if (arg1) {
          if (arg2) {
            const sjbqktjListv = resp.sjbqktjList ? resp.sjbqktjList : [];
            if (!sjbqktjListv.length) {
              return
            }
            this.kdsjbList = sjbqktjListv.map((obj) => {
              const yzbmssjbslv = obj.yzbmssjbsl; // 应在保密室试卷包数量
              const szbmssjbslv = obj.szbmssjbsl; // 实际在保密室试卷包数量
              const yzkdsjbslv = obj.yzkdsjbsl; // 应在考点试卷包数量
              const szkdsjbslv = obj.szkdsjbsl; // 实在考点试卷包数量
              return {
                kdmc: obj.kdmc,
                zs: obj.sjbzsl,
                yz_bms: yzbmssjbslv,
                sz_bms: szbmssjbslv,
                qs_bms: yzbmssjbslv - szbmssjbslv,
                yz_kd: yzkdsjbslv,
                sz_kd: szkdsjbslv,
                qs_kd: yzkdsjbslv - szkdsjbslv
              }
            });
          }
        }
      });
    });
  }


  /**
   * 组件在 显示面板中的位置+ 大小
   */
  initcompPositionSize_ctrshow(): void {
    // 位置 大小
    this.compw = this.o.compw + "px";
    this.comph = this.o.comph + "px";
    this.topv = this.o.topv + "px";
    this.leftv = this.o.leftv + "px";
    // 显示
    this.frompage = this.o.frompage;
    if (this.frompage === "bmskd") {
      this.getdata();
      this.lockid = setInterval(() => {
        this.getdata();
      }, this.intervalnum)

    }
  }


  @Input() o: any;
  @Output() outhander: EventEmitter<any> = new EventEmitter();

  constructor(private commservice: CommService,
              private localStorageService: LocalStorageService,
              private itservice: MyhttpService,
              private sharedService: SharedService) {
    this.sharedService.changeEmitted$.subscribe(resp => {
      const actiontype = resp.actiontype;
      const datav = resp.data;
      switch (actiontype) {
        case "activeNode": {
          const activebmsOrkd = datav.activebmsOrkd; // 高亮的节点 （考试管理机构/ 保密室 / 考点）
          const kdbhsv = datav.kdbhs;
          if (!activebmsOrkd) {
            return
          }
          const activenode_text = activebmsOrkd.text;
          const activenode_type = activebmsOrkd.type;
          if (activenode_type == "ksgljg") {
            return
          }
          this.comptitle = `(${activenode_text})`; //bms 或者  考点
          this.s_kdbhlist = kdbhsv;
          this.getdata();
        }
          break;
      }
    });
  }

  ngOnInit() {
    this.commservice.log("-- 【 compkey=bmssjxx】--被父组件动态加载init....", this.o);
    this.s_ksjhbh = this.localStorageService.get("hksjh");
    this.initcompPositionSize_ctrshow();
  }

  ngOnDestroy() {
    if (this.lockid) {
      clearInterval(this.lockid);
    }
  }

}
